//////////////////////////////////
///生成时间:2012-03-06 10:02:58
///BaseDAL:Cms分类(Cms_Type)(Cms_Type)
//////////////////////////////////
using System;
using System.Collections.Generic;

namespace yule.DAL.Cms
{
    /// <summary>
    /// Cms分类(Cms_Type) 数据访问类
    /// </summary>
    public partial class Type : yule.DAL.BaseDAL<yule.Model.Cms.Type>, yule.IDAL.Cms.IType
    {

        /// <summary>
        /// 查询Cms分类(Cms_Type)
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        protected override yule.Model.Cms.Type SelectFromDB(dynamic id)
        {
            yule.Model.Cms.Type model = null;

            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append(string.Format("select * from {0} where {1}='{2}'", yule.Model.Cms.Type.Table.Name, yule.Model.Cms.Type.Field.Id, id));

            System.Data.DataTable dt = this.DBHelper.ExecuteDataTable(sql.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                model = new yule.Model.Cms.Type();

                model.Convert(dt.Rows[0]);

                this.SetCache<yule.Model.Cms.Type>(model);
            }

            return model;
        }

        /// <summary>
        /// 新增Cms分类(Cms_Type)
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Insert(yule.Model.Cms.Type model, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append(string.Format(" INSERT INTO {0}", yule.Model.Cms.Type.Table.Name));
            sql.Append(" (");
            sql.Append(" " + yule.Model.Cms.Type.Field.Id);
            sql.Append(" ," + yule.Model.Cms.Type.Field.ParentId);
            sql.Append(" ," + yule.Model.Cms.Type.Field.PathId);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Name);
            sql.Append(" ," + yule.Model.Cms.Type.Field.ParentName);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Code);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Sort);
            sql.Append(" )");
            sql.Append(" VALUES");
            sql.Append(" (");
            sql.Append(" @" + yule.Model.Cms.Type.Field.Id);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.ParentId);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.PathId);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.Name);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.ParentName);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.Code);
            sql.Append(" ,@" + yule.Model.Cms.Type.Field.Sort);
            sql.Append(" )");


            System.Data.Common.DbParameter[] parameters = 
            {
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Id, yule.Util.Convert.ToString(model.Id) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.ParentId, yule.Util.Convert.ToString(model.ParentId) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.PathId, yule.Util.Convert.ToString(model.PathId) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Name, yule.Util.Convert.ToString(model.Name) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.ParentName, yule.Util.Convert.ToString(model.ParentName) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Code, yule.Util.Convert.ToString(model.Code) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Sort, yule.Util.Convert.ToInt(model.Sort) , System.Data.DbType.AnsiString),
            };

            return dbHelper.ExecuteNonQuery(sql.ToString(), parameters, (cmdText, paras) =>
            {
                this.SetCache<yule.Model.Cms.Type>(model);
            });

        }

        /// <summary>
        /// 删除Cms分类(Cms_Type)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Delete(string id, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append(string.Format("delete from {0} where {1} = '{2}'", yule.Model.Cms.Type.Table.Name, yule.Model.Cms.Type.Field.Id, id));

            return dbHelper.ExecuteNonQuery(sql.ToString(), (cmdText) =>
            {
                this.RemoveCache<yule.Model.Cms.Type>(id);
            });
        }

        /// <summary>
        /// 更新Cms分类(Cms_Type)
        /// </summary>
        /// <param name="insertModel"></param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Update(yule.Model.Cms.Type model, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append(string.Format("update {0}", yule.Model.Sys.Region.Table.Name));
            sql.Append(" SET");
            sql.Append(" " + yule.Model.Cms.Type.Field.Id + "=@" + yule.Model.Cms.Type.Field.Id);
            sql.Append(" ," + yule.Model.Cms.Type.Field.ParentId + "=@" + yule.Model.Cms.Type.Field.ParentId);
            sql.Append(" ," + yule.Model.Cms.Type.Field.PathId + "=@" + yule.Model.Cms.Type.Field.PathId);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Name + "=@" + yule.Model.Cms.Type.Field.Name);
            sql.Append(" ," + yule.Model.Cms.Type.Field.ParentName + "=@" + yule.Model.Cms.Type.Field.ParentName);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Code + "=@" + yule.Model.Cms.Type.Field.Code);
            sql.Append(" ," + yule.Model.Cms.Type.Field.Sort + "=@" + yule.Model.Cms.Type.Field.Sort);
            sql.Append(" WHERE");
            sql.Append(" " + yule.Model.Cms.Type.Field.Id + "=@" + yule.Model.Cms.Type.Field.Id); ;

            System.Data.Common.DbParameter[] parameters = 
            {
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Id, yule.Util.Convert.ToString(model.Id) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.ParentId, yule.Util.Convert.ToString(model.ParentId) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.PathId, yule.Util.Convert.ToString(model.PathId) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Name, yule.Util.Convert.ToString(model.Name) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.ParentName, yule.Util.Convert.ToString(model.ParentName) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Code, yule.Util.Convert.ToString(model.Code) , System.Data.DbType.AnsiString),
                dbHelper.CreateDbParameter("@" + yule.Model.Cms.Type.Field.Sort, yule.Util.Convert.ToInt(model.Sort) , System.Data.DbType.AnsiString),
            };


            return dbHelper.ExecuteNonQuery(sql.ToString(), parameters, (cmdText, paras) =>
            {
                this.RemoveCache<yule.Model.Cms.Type>(model);
            });
        }

        /// <summary>
        /// 查询所有Cms分类(Cms_Type)
        /// </summary>
        /// <returns></returns>
        public IList<yule.Model.Cms.Type> List()
        {
            yule.Data.IExecuteQuery dbHelper = yule.Data.DBHelper.Instance;

            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append("select * from " + yule.Model.Cms.Type.Table.Name);

            System.Data.DataTable dt = dbHelper.ExecuteDataTable(sql.ToString());

            IList<yule.Model.Cms.Type> list = this.ConverToList<yule.Model.Cms.Type>(dt,
                (model) => { this.SetCache<yule.Model.Cms.Type>(model); });

            return list;
        }

    }
}